﻿#region

using System;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using InfoTech.VinhSon.Bussiness;
using VinhSon.Models;

#endregion

namespace VinhSon.Controllers
{
    public class HomeController : BasePageController
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            Config config = UnitOfWork.ConfigRepository.SelectbyId(Constant.HOME_TITLE);
            ViewBag.Title = config != null
                                    ? config.Value
                                    : "Vinhson.org";
            config = UnitOfWork.ConfigRepository.SelectbyId(Constant.HOME_LINK);
            ViewBag.Link = config != null
                                   ? config.Value
                                   : "/Trang-Chu";
            config = UnitOfWork.ConfigRepository.SelectbyId(Constant.HOME_LINK_TOOLTIP);
            ViewBag.LinkTooltip = config != null
                                   ? config.Value
                                   : "";
            ViewBag.Top10Articles = UnitOfWork.ArticleRepository.Select(c => c.Inactive == false && c.Activated == true,
                                                                        c => c.LastModified,
                                                                        ListSortDirection.Descending)
                                              .Take(10)
                                              .ToList();
            return View();
        }

        public ActionResult Home()
        {
            Config config = UnitOfWork.ConfigRepository.SelectbyId(Constant.HOME_TITLE);
            ViewBag.Title = config != null
                                    ? config.Value
                                    : "Vinhson.org";
            ViewBag.TopArticles = UnitOfWork.ArticleRepository.Select(c => c.Inactive == false && c.Activated == true,
                                                                       c => c.LastModified,
                                                                       ListSortDirection.Descending)
                                             .Take(15)
                                             .ToList();
            return View(UnitOfWork.CategoryRepository.Select(c => c.Inactive == false && c.Activated == true && c.GroupIndex > 0)
                                  .ToList());
        }

        public ActionResult Menu()
        {
            return PartialView(UnitOfWork.CategoryRepository.Select(c => c.IsShowMenu == true && c.Activated == true && c.Inactive == false && c.Parent == null,
                                                                    "Categories.Categories")
                                         .ToList());
        }

        public ActionResult Category(int id)
        {
            return PartialView(UnitOfWork.ArticleRepository.Select(c => c.Category.Id == id && c.Activated == true && c.Inactive == false)
                                         .OrderByDescending(c => c.LastModified)
                                         .Take(5)
                                         .ToList());
        }

        public ActionResult Logo()
        {
            Config slogan = UnitOfWork.ConfigRepository.SelectbyId(Constant.HOME_SLOGAN);
            if (slogan != null)
                ViewBag.Slogan = slogan.Value;
            return PartialView(UnitOfWork.ConfigRepository.SelectbyId(Constant.HOME_BANNER));
        }

        public ActionResult Footer()
        {
            Config config = UnitOfWork.ConfigRepository.SelectbyId(Constant.HOME_FOOTER);
            if (config != null)
                return PartialView(config);
            return Content("");
        }

        public ActionResult RightColumn()
        {
            return PartialView(UnitOfWork.GroupRepository.Select(c => (c.FromDate == null || c.FromDate <= DateTime.Now) && (c.ToDate == null || c.ToDate >= DateTime.Now) && c.HomePage == true && c.Activated == true && (c.Inactive == false || c.Inactive == null),
                                                                 "Layout"));
        }

        public ActionResult IndexRightColumn()
        {
            return PartialView(UnitOfWork.GroupRepository.Select(c => (c.FromDate == null || c.FromDate <= DateTime.Now) && (c.ToDate == null || c.ToDate >= DateTime.Now) && c.Index == true && c.Activated == true && (c.Inactive == false || c.Inactive == null),
                                                                "Layout"));
        }
    }
}
